/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.information.biz.conv;

import com.fulihui.information.dal.dataobj.InfoGoodFeedbackRecord;
import com.fulihui.information.dto.InfoGoodFeedbackRecordDTO;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 有益反馈 转换工具类
 * @author zhangyingjie
 * @version $Id: v 0.1 2017年05月15日 15:14 zhangyingjie Exp $
 */
public class GoodFeedbackConv {
    /**
     * 不copy的属性数组
     */
    private static final String[] ignoreProperties = {};

    /**
     * DTO转换成DO
     *
     * @param source
     * @return
     */
    public static InfoGoodFeedbackRecord convertDTO2DO(InfoGoodFeedbackRecordDTO source) {
        if (source == null) {
            return null;
        }
        InfoGoodFeedbackRecord target = new InfoGoodFeedbackRecord();
        BeanUtils.copyProperties(source, target);
        target.setGmtModified(new Date());
        try {
            if (StringUtils.isNotEmpty(target.getDescript())) {
                target.setDescript(Base64.encodeBase64String(target.getDescript().getBytes("UTF-8")));
            }
        } catch (UnsupportedEncodingException e) {

        }
        return target;
    }

    /**
     * DO转换成DTO
     *
     * @param source
     * @return
     */
    public static InfoGoodFeedbackRecordDTO convertDO2DTO(InfoGoodFeedbackRecord source) {
        if (source == null) {
            return null;
        }
        InfoGoodFeedbackRecordDTO target = new InfoGoodFeedbackRecordDTO();
        BeanUtils.copyProperties(source, target);
        try {
            if (StringUtils.isNotEmpty(target.getDescript())) {
                target.setDescript(new String(Base64.decodeBase64(target.getDescript()), "UTF-8"));
            }
        } catch (UnsupportedEncodingException e) {

        }
        return target;
    }

    /**
     * doList转换为dtoList
     *
     * @param list
     * @return
     */
    public static List<InfoGoodFeedbackRecordDTO> convertdo2dtoList(List<InfoGoodFeedbackRecord> list) {
        return list.stream().map(GoodFeedbackConv::convertDO2DTO)
                .collect(Collectors.toList());

    }
    /**
     * dtoList转换为doList
     * @param list
     * @return
     */
    public static List<InfoGoodFeedbackRecord> convertdto2doList(List<InfoGoodFeedbackRecordDTO> list) {
        return list.stream().map(GoodFeedbackConv::convertDTO2DO)
                .collect(Collectors.toList());
    }
}
